package group.sicher.web.framework.home;

import group.sicher.core.exception.ResponseException;
import group.sicher.web.framework.home.serice.MarsLockService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

/**
 * Created by aronj on 2018/11/30.
 */
@Controller
@RequestMapping("/uuid")
public class AronHomeController {


    @Autowired
    private MarsLockService marsLockService;

    @RequestMapping(value = "/home", method = {RequestMethod.GET})
    public ModelAndView home(ModelAndView modelAndView) {
        modelAndView.setViewName("uuid");
        return modelAndView;
    }


    @RequestMapping(value = "/down", method = {RequestMethod.POST})
    public void down(HttpServletRequest request, HttpServletResponse response) {
        downLoadLcal(response, "");
    }


    @PostMapping(value = "/getudid")
    public void getudid(HttpServletRequest request) {

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                marsLockService.methodTest("t1", 1);
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                marsLockService.methodTest("t2", 2);
            }
        });

        thread1.start();
        thread2.start();
    }


    /**
     * 下载本地文件
     *
     * @param response HttpServletResponse
     * @param path     本地文件路径
     */
    public static void downLoadLcal(HttpServletResponse response, String path) {
        InputStream inputStream = null;
        OutputStream out = null;
        try {
            File file = ResourceUtils.getFile("classpath:static/udid.mobileconfig");
            inputStream = new FileInputStream(file);
            String fileName = file.getName();

            setFileName(fileName, response);
            //激活下载操作
            OutputStream os = response.getOutputStream();

            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            long downloadedLength = 0;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }

            os.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(out);
        }
    }


    /**
     * 文件名的定义
     * 返回response的定义
     *
     * @param fileName
     * @param response
     */
    private static void setFileName(String fileName, HttpServletResponse response) throws IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.addHeader("Access-Control-Max-Age", "1800");
        response.setContentType("application/x-apple-aspen-config;charset=utf-8");
        response.setHeader("Content-type", "application/x-apple-aspen-config");
        response.setHeader("chatset", "chatset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    }

}
